//
//  HMComposeBottomView.swift
//  WB25
//
//  Created by HM on 2017/7/8.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
// 按钮枚举
enum HMComposeBottomViewType: Int {
    case picture = 1001
    case mention
    case trend
    case emoticon
    case add
}

class HMComposeBottomView: UIView {
    
    // 第四个按钮
    var emoticonButton: UIButton?
    
    // 01-声明闭包
    var closure:((HMComposeBottomViewType)->())?
    
    // 判断是否是自定义表情键盘
    //             - 首次的时候显示的是笑脸 点击后 键盘变为表情键盘 按钮图片变成键盘
    //             - 如果是键盘按钮 点击后 键盘变为系统键盘 按钮图片变为表情
    var isEmoticon: Bool = false{
        didSet{
            // true 键盘是表情键盘 图片显示系统键盘图片
            // false 键盘是系统键盘 图片是笑脸
            // 记录图片名字
            let imgName = (isEmoticon == true) ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            
            emoticonButton?.setImage(UIImage(named:imgName), for: UIControlState.normal)
            emoticonButton?.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        let pictureButton = addChildButton(imgName: "compose_toolbar_picture", type: .picture)
        let mentionButton = addChildButton(imgName: "compose_mentionbutton_background", type: .mention)
        let trendButton = addChildButton(imgName: "compose_trendbutton_background", type: .trend)
        emoticonButton = addChildButton(imgName: "compose_emoticonbutton_background", type: .emoticon)
        let addButton = addChildButton(imgName: "compose_add_background", type: .add)
        // 添加约束
        pictureButton.snp.makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureButton.snp.right)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionButton.snp.right)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendButton.snp.right)
            make.width.equalTo(addButton)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(emoticonButton!.snp.right)
        }
    }
}

extension HMComposeBottomView{
    // 创建按钮的公共方法
    fileprivate func addChildButton(imgName: String, type: HMComposeBottomViewType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), for: UIControlState.highlighted)
        button.setImage(UIImage(named:imgName), for: UIControlState.normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), for: UIControlState.highlighted)
        addSubview(button)
        return button
    }
}

extension HMComposeBottomView{
    @objc fileprivate func buttonClick(button: UIButton){
        // 03-执行闭包
        // 通过枚举值得到枚举名HMComposeBottomViewTypeHMComposeBottomViewType(rawValue:Int)
        closure?(HMComposeBottomViewType(rawValue: button.tag)!)
    }
}
